%Computation of signal and noise power in E fields
% not tested
% Reference :Gamble, Goubau & Clarke 1978. Error analysis for rr mt, Geophysics 44 959-968
% Latest date 19.4.3

function[E] = Esgns(a,b,data,flag),


% a & b are corresponding impedance (Zxx Zxy) or (Zyx Zyy)
% data is array of cross spectra - SPMmatrix
% 
%flag should be either 'Ex' or 'Ey';

j = sqrt(-1);

ExEx = data(4,4);
EyEy = data(5,5);

HxEx = data(4,1) - j*data(1,4);
ExHx = conj(HxEx);
HxEy = data(5,1) - j*data(1,5);
EyHx = conj(HxEy);
ExHy = data(4,2) + j*data(2,4);
HyEx = conj(ExHy);
HyEy = data(5,2) - j*data(2,5);
EyHy = conj(HyEy);



if flag == 'Ex',
   ZpZ = a*HxEx + b*HyEx;
   E(1)=(ZpZ*ExEx+ExEx*conj(ZpZ))/2; % Signal power
   E(2)=ExEx-E(1);% Noise power
elseif flag == 'Ey',
   ZpZ = a*HxEy + b*HyEy;
   E(1)=(ZpZ*EyEy+EyEy*conj(ZpZ))/2;% Signal power
   E(2)=EyEy-E(1);% Noise power
else,
   E(1)=1;
   E(2)=0;
   fprintf('Esgns-> Allowed flags Ex and Ey');
end;
